/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.servlet.admin;

import cn.eclass.bean.api.IAdCategoryBean;
import cn.eclass.bean.api.IAdvertiseBean;
import cn.eclass.entity.AdCategoryEntity;
import cn.eclass.entity.AdvertiseEntity;
import cn.eclass.servlet.ActionFace;
import cn.eclass.utils.BeanFactory;
import cn.eclass.utils.Helper;
import com.google.gson.Gson;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;

/**
 *
 * @author fortruth
 */
public class Advertise extends ActionFace {

    IAdvertiseBean bean = BeanFactory.factory.getAdvertiseBean();

    @Override
    public void doIndex() {
        try {
            IAdCategoryBean adCategoryBean = BeanFactory.factory.getAdCatBean();
            Integer adCategoryId = Integer.valueOf(request.getParameter("adcatid"));
            AdCategoryEntity adCategory = adCategoryBean.get(adCategoryId);
            List<AdvertiseEntity> entities = bean.list(adCategory);
            List<AdCategoryEntity> adCategories = adCategoryBean.list();

            Field[] fields = AdvertiseEntity.class.getDeclaredFields();
            List<HashMap<String, String>> theFields = Helper.buildField(fields, module);

            request.setAttribute("adcategory", adCategory);
            request.setAttribute("adcategories", adCategories);
            request.setAttribute("entities", entities);
            request.setAttribute("fields", theFields);
            request.getRequestDispatcher(adminView).forward(request, response);
        } catch (ServletException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void doCreate() {
        AdvertiseEntity advertise = new AdvertiseEntity();
        bean.add(form2Entity(advertise));        
        this.doIndex();
    }

    /*
     * ajax action
     */
    public void doSelect() {
        int id = Integer.valueOf(request.getParameter("id"));
        AdvertiseEntity advertise = bean.get(id);
        out.print(new Gson().toJson(advertise));
    }

    public void doUpdate() {
        int id = Integer.valueOf(request.getParameter("id"));
        AdvertiseEntity advertise = bean.get(id);
        bean.update(form2Entity(advertise));
        this.doIndex();
    }

    public void doDelete() {
        int id = Integer.valueOf(request.getParameter("id"));
        bean.delete(id);
        this.doIndex();
    }

    private AdvertiseEntity form2Entity(AdvertiseEntity entity) {
        entity.setDescription(Helper.toUTF8(request.getParameter("description")));
        entity.setLink(Helper.toUTF8(request.getParameter("link")));
        entity.setPicture(Helper.getUploadFilePath(request, config, module, "picture"));
        entity.setAdcatId(Integer.valueOf(request.getParameter("adcatId")));
        entity.setOrder(Integer.valueOf(request.getParameter("order")));
        return entity;
    }
}
